package cn.szq.concurrent.thread.stop;

/**
 * @ClassName: VolatileCanStop
 * @Description:
 * @Author: szq
 * @Date: 2021-09-26 16:25:03
 */
public class VolatileCanStop implements Runnable{

    private volatile boolean canceled = false;

    @Override
    public void run() {
        int num = 0;
        try{
            while (!canceled && num <=100000){
                if (num % 10 == 0){
                    System.out.println(num + "是10的倍数。");
                }
                num++;
                Thread.sleep(1);
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileCanStop volatileCanStop = new VolatileCanStop();
        Thread thread = new Thread(volatileCanStop);
        thread.start();
        Thread.sleep(3000);
        volatileCanStop.canceled = true;
    }
}
